package com.chj.comp.common.auth.controller;

import com.chj.comp.common.auth.service.IAuthTokenService;
import com.chj.comp.common.auth.vo.AuthTokenVO;
import com.chj.comp.common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：cuihui
 * @date ：Created in 2022/2/18
 * @description：token接口
 * @modified By：
 */
@RestController
@Api(tags = "token接口")
@RequestMapping("/token")
public class AuthTokenController {

    @Autowired
    private IAuthTokenService authTokenService;

    @ApiOperation(value = "获取token（密码模式）")
    @GetMapping(value = "/getTokenByPassword")
    public Result<AuthTokenVO> getTokenByPassword(@RequestParam @ApiParam(value = "用户名", required = true) String username,
                                        @RequestParam @ApiParam(value = "密码", required = true) String password) {
        try {
            AuthTokenVO authTokenVO = authTokenService.getTokenByPassword(username, password);
            return Result.ok(authTokenVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "获取token（客户端模式）")
    @GetMapping(value = "/getTokenByClient")
    public Result<AuthTokenVO> getTokenByClient(@RequestParam @ApiParam(value = "clientId", required = true) String clientId,
                                                  @RequestParam @ApiParam(value = "clientSecret", required = true) String clientSecret) {
        try {
            AuthTokenVO authTokenVO = authTokenService.getTokenByClient(clientId, clientSecret);
            return Result.ok(authTokenVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "获取token（短信验证码模式）")
    @GetMapping(value = "/getTokenBySms")
    public Result<AuthTokenVO> getTokenBySms(@RequestParam @ApiParam(value = "手机号", required = true) String phone,
                                                  @RequestParam @ApiParam(value = "验证码（默认：888888）", required = true) String code) {
        try {
            AuthTokenVO authTokenVO = authTokenService.getTokenBySms(phone, code);
            return Result.ok(authTokenVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    /**
     * STEP1：请求授权（http://125.74.92.6:9090/comp/auth/oauth/authorize?client_id=c1&response_type=code&state=test&scope=all&redirect_uri=http://125.74.92.6:9090/comp/auth/getAuthCodeCallBack）
     * STEP2：未登录的话，先登录
     * STEP3：登陆成功，请求第一步参数中的回调地址，code、state参数在链接后
     * STEP4：拿到code，请求该地址，注意client_id、client_secret、redirect_uri上下文保持一致，获取到的code只能用一次
     * @param code
     * @param redirectUri
     * @return
     */
    @ApiOperation(value = "获取token，步骤见代码注释（授权码模式）")
    @GetMapping(value = "/getTokenByCode")
    public Result<AuthTokenVO> getTokenByCode(@RequestParam @ApiParam(value = "授权码", required = true) String code,
                                             @RequestParam @ApiParam(value = "回调地址（数据库配置、获取授权码参数、此处，三者需保持一致）", required = true) String redirectUri) {
        try {
            AuthTokenVO authTokenVO = authTokenService.getTokenByCode(code, redirectUri);
            return Result.ok(authTokenVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 回调地址获取token（简单模式）
     * STEP1：请求授权（http://125.74.92.6:9090/comp/auth/oauth/authorize?client_id=c1&response_type=token&state=test&scope=all&redirect_uri=http://125.74.92.6:9090/comp/auth/token/getTokenBySimpleCallBack）
     * STEP2：未登录的话，先登录
     * STEP3：登陆成功，请求第一步参数中的回调地址（即该地址），token信息在链接后以url hash形式返回，接口无法提取
     * @return
     */
    @ApiOperation(value = "回调地址获取token，步骤见代码注释（简单模式）")
    @GetMapping(value = "/getTokenBySimpleCallBack")
    public Result getTokenBySimpleCallBack() {
        try {
            return Result.ok("token信息在链接后以url hash形式返回，接口无法提取", "token在前台提取，适用于无后台服务的第三方应用");
        } catch (Exception e) {
            return Result.failed(e.getMessage());
        }
    }

    @ApiOperation(value = "刷新token")
    @GetMapping(value = "/refreshToken")
    public Result<AuthTokenVO> refreshToken(@RequestParam @ApiParam(value = "刷新token", required = true) String refresh_token) {
        try {
            AuthTokenVO authTokenVO = authTokenService.refreshToken(refresh_token);
            return Result.ok(authTokenVO);
        } catch (Exception e) {
            return Result.failed(e.getMessage());
        }
    }
}
